package com.gnerv.pabu.basic.core.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gnerv.pabu.basic.core.entity.Notice;
import com.gnerv.pabu.basic.core.service.INoticeService;
import com.gnerv.pabu.basic.core.utils.UUIDUtils;
import com.gnerv.pabu.basic.core.dto.NoticeDTO;
import com.gnerv.pabu.basic.core.dto.NoticeSaveDTO;
import com.gnerv.pabu.basic.core.dto.NoticeSearchDTO;
import com.gnerv.pabu.basic.core.mapper.NoticeMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 基础平台-通知公告表(ManageNotice)表服务实现类
 *
 * @author makejava
 * @since 2020-03-04 23:01:03
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Resource
    private NoticeMapper noticeMapper;

    @Override
    public boolean saveNotice(NoticeSaveDTO noticeSaveDTO) {
        if(StringUtils.isEmpty(noticeSaveDTO.getUkId())){
            createNotice(noticeSaveDTO);
        }else {
            updateNotice(noticeSaveDTO);
        }
        return true;
    }

    @Override
    public boolean createNotice(NoticeSaveDTO noticeSaveDTO) {
        Notice notice = noticeSaveDTO.convert();
        notice.setUkId(UUIDUtils.getLower());
        if (!StringUtils.isEmpty(notice.getStatus()) && notice.getStatus() == 1) {
            notice.setReleaseTime(LocalDateTime.now());
        }
        noticeMapper.insert(notice);
        return true;
    }

    @Override
    public boolean deleteByNoticeId(String noticeId) {
        noticeMapper.deleteById(noticeId);
        return true;
    }

    @Override
    public boolean updateNotice(NoticeSaveDTO noticeSaveDTO) {
        Notice notice = noticeSaveDTO.convert();
        if (!StringUtils.isEmpty(notice.getStatus()) && notice.getStatus() == 1) {
            notice.setReleaseTime(LocalDateTime.now());
        }
        noticeMapper.updateById(notice);
        return true;
    }

    @Override
    public boolean updateNoticeStatus(String noticeId, Integer status) {
        LocalDateTime releaseTime = null;
        if (status == 1) {
            releaseTime = LocalDateTime.now();
        }
        noticeMapper.updateNoticeStatus(noticeId, status, releaseTime);
        return true;
    }

    @Override
    public NoticeDTO selectByNoticeId(String noticeId) {
        NoticeDTO notice = noticeMapper.selectByNoticeId(noticeId);
        return notice;
    }

    @Override
    public Page<NoticeDTO> listNotice(NoticeSearchDTO notice, Long current, Long size) {
        Page<NoticeDTO> notices = noticeMapper.listNotice(new Page(current, size), notice);
        return notices;
    }
}